<template>
  <ele-page>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="roleId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        cache-key="systemRoleTable"
        :pagination="false"
      >
        <template #toolbar>
          <el-button
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>
        <template #action="{ row }">
          <el-link type="primary" :underline="false" @click="openEdit(row)">
            修改
          </el-link>
          <el-divider direction="vertical" />
          <el-link type="danger" :underline="false" @click="remove(row)">
            删除
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit
      v-model="showEdit"
      :data="current"
      :job-position-id="positionId"
      @done="reload"
    />
  </ele-page>
</template>

<script setup>
  import { ref, unref, watch } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';
  import Edit from './components/edit.vue';

  import { pageShifts, removeShifts } from '@/api/company';
  import { useRouter } from 'vue-router';
  const { currentRoute } = useRouter();
  const ROUTE_PATH = '/company/shifts';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'title',
      label: '部门名称',
      width: 150
    },
    {
      prop: 'start',
      label: '开始时间',
      width: 150
    },
    {
      prop: 'end',
      label: '结束时间',
      width: 150
    },
    {
      columnKey: 'action',
      label: '操作',
      slot: 'action'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 企业id */
  const positionId = ref(0);

  const getId = () => {
    const { query } = unref(currentRoute);
    positionId.value = Number(query.id);
  };
  getId();

  /** 表格数据源 */
  const datasource = ({ page, where, orders }) => {
    return pageShifts({
      ...where,
      ...orders,
      page,
      limit: 999999999,
      job_position_id: positionId.value
    });
  };

  /** 搜索 */
  const reload = (where) => {
    const { query } = unref(currentRoute);
    positionId.value = Number(query.id);
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 删除单个 */
  const remove = (row) => {
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    ElMessageBox.confirm(
      '确定要删除“' + rows.map((d) => d.title).join(', ') + '”吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        const id = row.id;
        removeShifts({ id })
          .then((msg) => {
            loading.close();
            EleMessage.success(msg);
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
  watch(
    currentRoute,
    (route) => {
      if (unref(route).path !== ROUTE_PATH) {
        return;
      }
      reload();
    },
    { immediate: true }
  );
</script>

<script>
  export default {
    name: 'SystemRole'
  };
</script>
